package com.biyu.monitormanage.model.bo;

import com.biyu.monitormanage.entity.RealTimeDataEntity;
import com.biyu.monitormanage.enums.MonitorStatusEnums;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Objects;

/**
 * 参数封装的数据对象
 * @author 15491
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MachineMonitorBo {

    /**
     * 参数类型
     */
    private String paramKey;
    /**
     * 参数值
     */
    private String paramValue;
    /**
     * 显示图片参数
     */
    private Integer imageParam;

    /**
     * 封装到Bo对象列表中
     * @param machineMonitorBos 提前新建的空列表
     * @param realTimeDataEntity 实时数据实体
     */
    public static void addMachineMonitorBo(List<MachineMonitorBo> machineMonitorBos,
                                           RealTimeDataEntity realTimeDataEntity) {
        addMachineMonitorBo(machineMonitorBos, realTimeDataEntity, null);
    }

    /**
     * 封装到Bo对象列表中
     * @param machineMonitorBos 提前新建的空列表
     * @param realTimeDataEntity 实时数据实体
     * @param machineMonitorName  被检测机器名称
     */
    public static void addMachineMonitorBo(List<MachineMonitorBo> machineMonitorBos,
                                           RealTimeDataEntity realTimeDataEntity,
                                           String machineMonitorName) {
        if (Objects.isNull(realTimeDataEntity)) {
            return;
        }
        Float resOfTemHum;
        Integer res;
        if (Objects.nonNull(resOfTemHum = realTimeDataEntity.getTemperature())) {
            machineMonitorBos.add(MachineMonitorBo.builder()
                            .paramKey(getParamKey(machineMonitorName, "   温度"))
                            .paramValue(resOfTemHum.toString() + "℃")
                            .imageParam(1)
                    .build());
        }
        if (Objects.nonNull(resOfTemHum = realTimeDataEntity.getHumidity())) {
            machineMonitorBos.add(MachineMonitorBo.builder()
                    .paramKey(getParamKey(machineMonitorName, "   湿度"))
                    .paramValue(resOfTemHum.toString() + "%")
                            .imageParam(2)
                    .build());
        }
        if (Objects.nonNull(res = realTimeDataEntity.getSmokeSensor())) {
            machineMonitorBos.add(MachineMonitorBo.builder()
                    .paramKey(getParamKey(machineMonitorName, "   烟雾传感器"))
                    .paramValue(MonitorStatusEnums.getDesc(res))
                            .imageParam(3)
                    .build());
        }
        if (Objects.nonNull(res = realTimeDataEntity.getWaterLeakage()) && Objects.equals(res, 0)) {
            machineMonitorBos.add(MachineMonitorBo.builder()
                    .paramKey(getParamKey(machineMonitorName, "   漏水监测仪"))
                    .paramValue(MonitorStatusEnums.getDesc(res))
                            .imageParam(4)
                    .build());
        }
        if (Objects.nonNull(res = realTimeDataEntity.getInfraRed()) && Objects.equals(res, 0)) {
            machineMonitorBos.add(MachineMonitorBo.builder()
                    .paramKey(getParamKey(machineMonitorName, "   红外监测器"))
                    .paramValue(MonitorStatusEnums.getDesc(res))
                            .imageParam(5)
                    .build());
        }
    }

    private static String getParamKey(String machineMonitorName, String paramKey) {
        return Objects.nonNull(machineMonitorName)? machineMonitorName + paramKey
               : paramKey;
    }
}
